package boardgamer.yavalath.client.pro;

import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import boardgamer.common.client.preference.PreferenceUpdater;
import boardgamer.common.client.preference.RegisteredEnumList;
import boardgamer.common.client.preference.StringConverter;
import boardgamer.yavalath.client.MainView;
import boardgamer.yavalath.client.ProFeatures;
import boardgamer.yavalath.client.YavalathActivity;
import boardgamer.yavalath.client.replay.ReplayScreen;
import boardgamer.yavalath.client.replaylobby.ReplayLobbyScreen;
import boardgamer.yavalath.common.YavalathGameSetting;
import boardgamer.yavalath.common.YavalathReplay;

import static boardgamer.yavalath.common.YavalathGameSetting.BoardSize;
import static boardgamer.yavalath.common.YavalathGameSetting.DEFAULT;
import static boardgamer.yavalath.common.YavalathGameSetting.Rule;
import static com.google.common.base.Preconditions.checkNotNull;

/**
 * @author garysoed
 */
public class ProProFeatures implements ProFeatures {

  private static final String BOARD_SIZE_KEY = "board-size";
  private static final String GAME_RULE_KEY = "game-rule";
  
  private MainView gameView;
  private YavalathActivity activity;
  private ReplayLobbyScreen replayLobbyScreen;

  @Override
  public boolean isEnabled() {
    return true;
  }

  @Override
  public void initializeScreens(MainView gameView, YavalathActivity activity) {
    this.gameView = checkNotNull(gameView, "view");
    this.activity = checkNotNull(activity, "activity");
    this.replayLobbyScreen = new ReplayLobbyScreen(activity);
    this.replayLobbyScreen.initialize(gameView.getHolder().getSurfaceFrame());
  }

  @Override
  public void startReplayLobbyView() {
    gameView.showScreen(replayLobbyScreen);
  }

  @Override
  public void startReplayView(YavalathReplay replay) {
    ReplayScreen replayScreen = new ReplayScreen(replay);
    replayScreen.initialize(gameView.getHolder().getSurfaceFrame());
    gameView.showScreen(replayScreen);
  }

  @Override
  public void startMultiplayerView() {
    throw new UnsupportedOperationException("TODO");
  }

  @Override
  public void initializePreference(PreferenceUpdater.Builder updaterBuilder) {
    updaterBuilder.registerEnumList(BOARD_SIZE_KEY,
        BoardSize.NORMAL,
        BoardSize.class,
        new StringConverter<BoardSize>() {
          @Override
          public String toString(BoardSize value) {
            return value.name();
          }

          @Override
          public BoardSize valueOf(String stringValue) {
            return BoardSize.valueOf(stringValue);
          }
        },
        new RegisteredEnumList.Renderer<BoardSize>() {
          @Override
          public String render(BoardSize object) {
            return object.render();
          }
        }
    );
    updaterBuilder.registerEnumList(GAME_RULE_KEY,
        Rule.LOSE_3_WIN_4,
        Rule.class,
        new StringConverter<Rule>() {
          @Override
          public String toString(Rule value) {
            return value.name();
          }

          @Override
          public Rule valueOf(String stringValue) {
            return Rule.valueOf(stringValue);
          }
        },
        new RegisteredEnumList.Renderer<Rule>() {
          @Override
          public String render(Rule object) {
            return object.render();
          }
        }
    );
  }

  @Override
  public YavalathGameSetting getGameSetting() {
    SharedPreferences sharedPreferences
        = PreferenceManager.getDefaultSharedPreferences(activity.getBaseContext());
    Rule rule = Rule.valueOf(sharedPreferences.getString(GAME_RULE_KEY, DEFAULT.getRule().name()));
    BoardSize boardSize = BoardSize.valueOf(
        sharedPreferences.getString(BOARD_SIZE_KEY, 
        DEFAULT.getBoardSize().name()));
    return new YavalathGameSetting(boardSize, rule);
  }
}
